$(function () {
    $.ajax({
        url: '../data/data1.json',
        type: 'get',
        datatype: 'json',
        success: function (data) {
            var strDom1 = ''
            $.each(data, function (index, item) {
                var pics = `
            
                <li>
                    <img src="${item.imgurl}" alt="" class="tupian">
                </li>
          
                `
                strDom1 += pics
            })
            $('.pic ul').html(strDom1)

        }
    })
    $.ajax({
        url: '../data/data2.json',
        type: 'get',
        datatype: 'json',
        success: function (data) {
            var strDom2 = ''
            $.each(data, function (index, item) {
                if (item.new) {
                    var goods = `
            <li data-id="${item.id}" data-num="${item.num}" class="xin">
                <p>${item.new}</p>
                <img src="${item.imgurl}" alt="">
                <a class="title">${item.title}</a>
                <a class="desc">${item.desc}</a>
                <a class="price">¥${item.price}</a>
            </li>
                ` } else {
                    var goods = `
                    <li data-id="${item.id}" data-num="${item.num}" class="xin">
                        <img src="${item.imgurl}" alt="">
                        <a  class="title">${item.title}</a>
                        <a  class="desc">${item.desc}</a>
                        <a  class="price">¥${item.price}</a>
                    </li> 
                    `
                }
                strDom2 += goods
            })
            $('.goods4-right ul').html(strDom2)
        }
    })
    if (localStorage.getItem('goods')) {
        var goodsArr = JSON.parse(localStorage.getItem('goods'))
    } else {
        var goodsArr = []
    }
   
    $('.drop').on("click", ".xin", function () {
        window.open('http://localhost:3001/look.html')
        var id = $(this).attr('data-id')
        var num = $(this).attr('data-num')
        var flag = false
        $.each(goodsArr, function (index, item) {
            item.key = "false"
            if (item.id === id) {
                console.log(goodsArr[0])
                flag = true
                item.num++
                num = item.num
                item.key = "ture"
            }
        })
        if (!flag) {
            goodsArr.push({ 'id': id, 'num': num, 'key': 'ture' })
        }
        localStorage.setItem('goods', JSON.stringify(goodsArr))
        if (localStorage.getItem('goumai')) {
            var goumaiArr = JSON.parse(localStorage.getItem('goumai'))
        } else {
            var goumaiArr = []
        }
        $.each(goumaiArr,function(index,item){
            $.each(goodsArr,function(ind,itm){
                if(item.id===itm.id){
                   itm.num = item.num
                }
            })
        })
        localStorage.setItem('goods', JSON.stringify(goodsArr))


    })
    window.onload = function () {
        if (localStorage.getItem('name')) {
            var nameArr = JSON.parse(localStorage.getItem('name'))
        } else {
            var nameArr = []
        }
        $.each(nameArr, function (index, item) {
            $('.login3').html(item.name)
            $('.p p').html("您好! " + item.name)
            $('.add').html('')
        })

    }
    $('.login3').on('click',function(){
        var urlArr = []
        var url1 = window.location.href
        console.log(url1)
        urlArr.push({'url':url1})
        localStorage.setItem('url',JSON.stringify(urlArr))
    })


})